/*
Simple Forum Auto Poster.
Copyright (C) 2011 Gianni Van Hoecke.

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package domain.error;

import domain.forumdata.Forum;
import java.util.ArrayList;

/**
 *
 * @author Gianni Van Hoecke <gianni.vh@gmail.com>
 * Contains a collection of thrown errors during posting.
 */
public class ErrorCollection
{
    private ArrayList<Error> errors;
    private int idCounter;

    /**
     * Default constructor. Initializes an empty error list.
     */
    public ErrorCollection()
    {
        this.errors = new ArrayList<Error>();
        this.idCounter = 1;
    }

    /**
     * Adds a new error to the list with an unique ID.
     * @param forum The forum object.
     * @param loginOK Whether the login was successful or not.
     * @param postOK Whether the post was successful or not.
     * @param error The returned HTML error page.
     */
    public void addError(Forum forum, boolean loginOK, boolean postOK, String error)
    {
        this.errors.add(new Error(this.idCounter++, forum, loginOK, postOK, error));
    }

    /**
     * @return The error list.
     */
    public ArrayList<Error> getErrors()
    {
        return errors;
    }

    /**
     * @return The error list as array.
     */
    public Object[][] getErrorsAsArray()
    {
        Object[][] list = new Object[this.errors.size()][5];

        for(int i = 0; i < this.errors.size(); i++)
        {
            Error error = this.errors.get(i);
            list[i][0] = new Integer(error.getId());
            list[i][1] = error.getForum().getForumName();
            list[i][2] = error.getForum().getForumType();
            list[i][3] = error.isLoginOK() ? "Succes!" : "Failed!";
            list[i][4] = error.isPostOK() ? "Succes!" : "Failed!";
        }
        
        return list;
    }

    /**
     * @param id The ID of the error.
     * @return The error object or null when not found.
     */
    public Error getErrorByID(int id)
    {
        for(Error error : this.getErrors())
        {
            if(error.getId() == id)
                return error;
        }
        return null;
    }
}
